/**   
* @Title: XStreamUtil.java
* @Package org.xaoyaoyao.spider.xstream
* @Description: TODO
* @author xaoyaoyao
* @date Jan 25, 2019 11:09:34 AM
* @version V1.0   
*/
package org.xaoyaoyao.spider.xstream;

import org.apache.commons.lang3.StringUtils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @ClassName: XStreamUtil
 * @Description: TODO
 * @author xaoyaoyao
 * @date Jan 25, 2019 11:09:34 AM
 * 
 */
public class XStreamUtil {
	/**
	 * @Title: xmlToBean
	 * @Description: 将xml字符串转化为java对象 (注解方式)
	 * @param xml
	 *            xml文件
	 * @param clazz
	 *            转换的类对象
	 * @return T
	 */
	public static <T> T xmlToBean(String xml, Class<T> clazz) {
		if (StringUtils.isNotBlank(xml) && null != clazz) {
			XStream xStream = new XStream(new DomDriver());
			xStream.processAnnotations(clazz);
			xStream.setClassLoader(clazz.getClassLoader());
			XStream.setupDefaultSecurity(xStream);
			xStream.allowTypesByWildcard(new String[] { clazz.getPackage().getName() + ".**" });
			Object object = xStream.fromXML(xml);
			T cast = clazz.cast(object);
			return cast;
		}
		return null;
	}
}
